﻿using EasyNet.TCP;
using EasyNet.TCP.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics.Eventing.Reader;
using System.Text.Json.Nodes;
using System.Windows.Forms;

namespace 水下机器人操作系统
{
    public class MyClient : ClientCore
    {
        protected override byte[] ByteArrayPKTDecryptor(byte[] data_)
        {
            return data_;
        }

        protected override byte[] ByteArrayPKTEncryptor(byte[] data_)
        {
            return data_;
        }

        protected override void ByteArrayPKTHandled(byte[] msg_)
        {
            
        }

        protected override void DecryptingByteArrayPKTException(Exception exception_)
        {
            
        }

        protected override void DecryptingFileInfoPKTException(Exception exception_)
        {
            
        }

        protected override void DecryptingFilePKTException(Exception exception_)
        {
            
        }

        protected override void DecryptingTextPKTException(Exception exception_)
        {
            
        }

        protected override byte[] FileInfoPKTDecryptor(byte[] data_)
        {
            return data_;
        }

        protected override byte[] FileInfoPKTEncryptor(byte[] data_)
        {
            return data_;
        }

        protected override void FileInfoPKTHandled(FileMsg file_msg)
        {
            
        }

        protected override byte[] FilePKTDecryptor(byte[] data_)
        {
            return data_;
        }

        protected override byte[] FilePKTEncryptor(byte[] data_)
        {
            return data_;
        }

        protected override void FilePKTHandled(FileMsg file_msg)
        {
            
        }

        protected override void FilePKTSent(FileMsg file_msg)
        {
            
        }

        protected override void FileReceived(FileMsg file_msg)
        {
            
        }

        protected override void FileReceivingStarted(FileMsg file_msg)
        {
            
        }

        protected override void FileSendingException(Exception exception_)
        {
            
        }

        protected override void FileSent(FileMsg file_msg)
        {
            
        }

        protected override void HandlingFileInfoPKTException(Exception exception_)
        {
            
        }

        protected override void HandlingFilePKTException(Exception exception_)
        {
            
        }

        protected override void HandlingTextPKTException(Exception exception_)
        {
            MessageBox.Show(exception_.ToString(), "接收失败");
        }

        protected override void ReceivingException(Exception exception_)
        {
            MessageBox.Show(exception_.ToString(), "接收失败");
        }

        protected override void ServerConnected()
        {
            Receive();
            JObject jObject = new JObject();
            jObject["Init"] = "Connect";
            string jsonStr = jObject.ToString();
            Constants.myClient.SendTextPKT(jsonStr);
            Constants.mainForm.Invoke(() => 
            {
                Constants.mainForm.btnConnect.Text = "已连接";
            });
        }

        protected override void ServerConnectingException(Exception exception_)
        {
            MessageBox.Show(exception_.ToString(), "服务器连接失败");
        }

        protected override void ServerDisconnected()
        {
            Constants.mainForm.Invoke(() =>
            {
                Constants.mainForm.btnConnect.Text = "连接";
            });
        }

        protected override byte[] TextPKTDecryptor(byte[] data_)
        {
            return data_;
        }

        protected override byte[] TextPKTEncryptor(byte[] data_)
        {
            return data_;
        }

        protected override void TextPKTHandled(string msg_)
        {
            JObject jObject = JObject.Parse(msg_);
            if (jObject.ContainsKey("Input01"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAngleX.Text = jObject["Input01"].ToString();
                });
            }
            if (jObject.ContainsKey("Input02"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAngleY.Text = jObject["Input02"].ToString();
                });
            }
            if (jObject.ContainsKey("Input03"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAngleZ.Text = jObject["Input03"].ToString();
                });
            }
            if (jObject.ContainsKey("Input04"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labVelocityX.Text = jObject["Input04"].ToString();
                });
            }
            if (jObject.ContainsKey("Input05"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labVelocityY.Text = jObject["Input05"].ToString();
                });
            }
            if (jObject.ContainsKey("Input06"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labVelocityZ.Text = jObject["Input06"].ToString();
                });
            }
            if (jObject.ContainsKey("Input07"))
            {

                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAccX.Text = jObject["Input07"].ToString();
                });
            }
            if (jObject.ContainsKey("Input08"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAccY.Text = jObject["Input08"].ToString();
                });
            }
            if (jObject.ContainsKey("Input09"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labAccZ.Text = jObject["Input09"].ToString();
                });
            }
            if (jObject.ContainsKey("Input10"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labWQValue.Text = jObject["Input10"].ToString();
                });
            }
            if (jObject.ContainsKey("Input11"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labWQAss.Text = jObject["Input11"].ToString();
                });
            }
            if (jObject.ContainsKey("Input12"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labBatteryCapacity.Text = jObject["Input12"].ToString();
                });
            }
            if (jObject.ContainsKey("Input13"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labLeavingMemory.Text = jObject["Input13"].ToString();
                });
            }
            if (jObject.ContainsKey("Input14"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labLeavingCapacity.Text = jObject["Input14"].ToString();
                });
            }
            if (jObject.ContainsKey("Input15"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labConnectionState.Text = jObject["Input15"].ToString();
                });
            }
            if (jObject.ContainsKey("Input16"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labSentCount.Text = jObject["Input16"].ToString();
                });
            }
            if (jObject.ContainsKey("Input17"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.labReceivedCount.Text = jObject["Input17"].ToString();
                });
            }
            if (jObject.ContainsKey("Input18"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM1U.Text = jObject["Input18"].ToString();
                });
            }
            if (jObject.ContainsKey("Input19"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM2U.Text = jObject["Input19"].ToString();
                });
            }
            if (jObject.ContainsKey("Input20"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM3U.Text = jObject["Input20"].ToString();
                });
            }
            if (jObject.ContainsKey("Input21"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM4U.Text = jObject["Input21"].ToString();
                });
            }
            if (jObject.ContainsKey("Input22"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM1V.Text = jObject["Input22"].ToString();
                });
            }
            if (jObject.ContainsKey("Input23"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM2V.Text = jObject["Input23"].ToString();
                });
            }
            if (jObject.ContainsKey("Input24"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM3V.Text = jObject["Input24"].ToString();
                });
            }
            if (jObject.ContainsKey("Input25"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labRPM4V.Text = jObject["Input25"].ToString();
                });
            }
            if (jObject.ContainsKey("Input26"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labWM1U.Text = jObject["Input26"].ToString();
                });
            }
            if (jObject.ContainsKey("Input27"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labWM2U.Text = jObject["Input27"].ToString();
                });
            }
            if (jObject.ContainsKey("Input28"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labWM1V.Text = jObject["Input28"].ToString();
                });
            }
            if (jObject.ContainsKey("Input29"))
            {
                Constants.ctrlForm.Invoke(() =>
                {
                    Constants.ctrlForm.labWM2V.Text = jObject["Input29"].ToString();
                });
            }
            if (jObject.ContainsKey("IngestServer"))
            {
                Constants.mainForm.Invoke(() =>
                {
                    Constants.mainForm.vlcCtrl.VlcMediaPlayer.Play(jObject["IngestServer"].ToString());
                });
            }
        }
    }
}
